package org.openkoala.koala.deploy.action;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.openkoala.koala.deploy.dialog.FileOpenDialog;
import org.openkoala.koala.deploy.tree.SelectionUI;
import org.openkoala.koala.deploy.tree.SoapWebServiceSelectionUI;
import org.openkoala.koala.deploy.webservice.pojo.WebServiceDeploy;
import org.openkoala.koala.deploy.webservice.util.WebServiceDeployCreate;
import org.openkoala.koala.exception.KoalaException;

public class SoapWebServiceDeployAction extends WebServiceDeployAction {

	private String result;

	@Override
	public void process() {
		try {
			ProgressMonitorDialog monitorDialog = new ProgressMonitorDialog(shell);
			monitorDialog.run(true, true, new IRunnableWithProgress() {
				@Override
				public void run(IProgressMonitor monitor)
						throws InvocationTargetException, InterruptedException {
					monitor.beginTask("处理及打包SOAP WebService...", 2);
					monitor.worked(1);
					monitor.setTaskName("打包SOAP WebService...");
					try {
						result = WebServiceDeployCreate.processSoap(getWebServiceDeploy());
					} catch (KoalaException e) {
						e.printStackTrace();
						MessageDialog.openError(shell,
								"ERROR", e.getMessage());
					}
					monitor.worked(2);
					monitor.done();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
		FileOpenDialog dialog = new FileOpenDialog(new Shell(
				Display.getCurrent(), SWT.CLOSE), "WebService服务部署", result);
		dialog.open();
	}

	@Override
	public void openDialog() {
		try{
			SelectionUI<WebServiceDeploy> selectionUI = new SoapWebServiceSelectionUI(new Shell(
					Display.getDefault()), this);
			selectionUI.initData(getWebServiceDeploy());
			selectionUI.open();
		}catch(Exception e){
			e.printStackTrace();
			MessageDialog.openError(shell,
					"ERROR", e.getMessage() + ":" + e.getClass().toString());
		}
	}

}
